package com.hmall.item.Tast;

import cn.hutool.json.JSONUtil;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.impl.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description Top20Tast
 * @Author zhengyuan&junjun
 * @Date 2024-11-15
 */
@Component
@Slf4j
public class Top20Tast {
/**
 * 定时任务实现每天凌晨3点执行一次任务：统计出表中销量Top20商品列表，保存到Redis缓存中
 *
 * **/
  @Autowired
   private StringRedisTemplate redisTemplate;
   @Autowired
   private ItemService service;

 @Scheduled(fixedDelay = 5000)

   public  void  getTimetop20(){
   //1.获取销量20的商品列表
      List<Item> top20 = service.getTop20();
      //1.1定义redis的key
      String top20key = "Top20";
      //1.2将top20放进redis
      redisTemplate.opsForValue().set(top20key, JSONUtil.toJsonStr(top20));
       log.info("定时器任务正在运行--------");


   }

}
